REM Kill Process
SET THISNAME=Lectrode's Process Killer v3.0.291
MODE CON COLS=60 LINES=20
TITLE %THISNAME%


:: -----SETTINGS-----
SET CUSTOMPROCESS1=sample.exe
SET CUSTOMPROCESS2=
SET CUSTOMPROCESS3=
SET CUSTOMPROCESS4=

:: Delay in seconds to wait before each check
SET CHECKDELAY=5



:: -----MAIN-PROGRAM-----
:START

::INITIALIZE
SETLOCAL ENABLEDELAYEDEXPANSION
SET PREVENTING=0
SET KILLS=0
SET PROCESS=
SET SPACES=25
SET GETTINGPARAMS=
:GETCMDPARAMS
IF NOT "%1"=="" SET PROCESS=%PROCESS%%PARAMSPACE%%1
IF NOT "%1"=="" SET PARAMSPACE= &SHIFT&GOTO :GETCMDPARAMS

IF NOT "%PROCESS%"=="" SET PREVENTING=1
IF NOT "%PROCESS%"=="" GOTO :B4CHECK

CALL :HEADER
CALL :CALC_NAMES
CALL :ASK4Process
CALL :KILLPROCESS
GOTO :START

:KILLPROCESS
CALL :HEADER
ECHO Killing %PROCESS%...
SET ATTEMPTS=0
:TRYKILLPROCESS
SET /A ATTEMPTS+=1
IF %ATTEMPTS% GEQ 6 GOTO :KILLFAILED
TASKKILL /F /T /IM "%PROCESS%"
IF ERRORLEVEL 1 GOTO :TRYKILLPROCESS
SET /A KILLS+=1
GOTO :EOF

:KILLFAILED
CALL :HEADER
ECHO Could not kill "%PROCESS%"
ECHO Please make sure you spelled the name 
ECHO correctly and have sufficient permissions.
CALL :SLEEP 4
GOTO :START


:ASK4Process
SET PREVENTING=0
SET PROCESS=
CALL :HEADER
CALL :SHOW_CUSTOM_PROCESSES
SET usrInput=
SET /P usrInput=[] 
IF /I "%usrInput%"=="x" EXIT
IF NOT "%usrInput%"=="" SET PROCESS=%usrInput%
IF /I "%usrInput%"=="1" SET PROCESS=%CUSTOMPROCESS1%
IF /I "%usrInput%"=="2" SET PROCESS=%CUSTOMPROCESS2%
IF /I "%usrInput%"=="3" SET PROCESS=%CUSTOMPROCESS3%
IF /I "%usrInput%"=="4" SET PROCESS=%CUSTOMPROCESS4%
IF /I "%usrInput%"=="XX" GOTO :ASK4Process2Prevent
IF "%PROCESS%"=="" GOTO :ASK4Process
GOTO :EOF

:ASK4Process2Prevent
SET PREVENTING=1
SET PROCESS=
CALL :HEADER
CALL :SHOW_CUSTOM_PROCESSES
SET usrInput=
SET /P usrInput=[] 
IF /I "%usrInput%"=="x" EXIT
IF NOT "%usrInput%"=="" SET PROCESS=%usrInput%
IF /I "%usrInput%"=="1" SET PROCESS=%CUSTOMPROCESS1%
IF /I "%usrInput%"=="2" SET PROCESS=%CUSTOMPROCESS2%
IF /I "%usrInput%"=="3" SET PROCESS=%CUSTOMPROCESS3%
IF /I "%usrInput%"=="4" SET PROCESS=%CUSTOMPROCESS4%
IF /I "%usrInput%"=="XX" GOTO :ASK4Process
IF "%PROCESS%"=="" GOTO :ASK4Process

:B4CHECK
CALL :CHECK4Process
CALL :SLEEP %CHECKDELAY%
GOTO :B4CHECK


:SLEEP
REM ------------------------PROGRAM SLEEP-------------------------
REM Program sleeps for %1 seconds
IF "%1"=="" SET pN=3
IF NOT "%1"=="" SET pN=%1
PING -n 2 -w 1000 127.0.0.1>NUL
PING -n %pN% -w 1000 127.0.0.1>NUL
GOTO :EOF
REM ------------------------END PROGRAM SLEEP---------------------


:CHECK4Process
CALL :HEADER
ECHO Checking for %PROCESS%...
TASKLIST | FIND "%PROCESS%">NUL
IF NOT ERRORLEVEL 1 CALL :KILLPROCESS
GOTO :EOF



:HEADER
@ECHO OFF
REM ---------------------PROGRAM STATUS-----------------------
CALL :CENTERTEXT 26 SHOWPROCESS %PROCESS%
CALL :CENTERTEXT 28 SHOWKILLS %KILLS%

CLS
						ECHO  **********************************************************
						ECHO  *                                                        *
						ECHO  *   ******  %THISNAME%   ******  *
						ECHO  *                                                        *
						ECHO  * http://code.google.com/p/useful-scripts-by-electrodexs *
						ECHO  *                                                        *
IF "%PREVENTING%"=="1"	ECHO  *--------------------------------------------------------*
IF "%PREVENTING%"=="1"	ECHO  *          PROCESS         ^|            KILLS            *
IF "%PREVENTING%"=="1"	ECHO  *%SHOWPROCESS%^|%SHOWKILLS% *
						ECHO  **********************************************************
						ECHO.
						ECHO.
GOTO :EOF

:CENTERTEXT
SET TTLSPACE=%1
SET VAR2SET=%2
SET TEXT=%3%
CALL :StrLength STRLEN %TEXT%


SET /A HALFSPACE=(TTLSPACE-STRLEN)/2
SET INCNUM=0
:ADDSPACEFRONT
SET /A INCNUM+=1
SET TEXT= %TEXT%
IF NOT %INCNUM% GEQ %HALFSPACE% GOTO :ADDSPACEFRONT

SET INCNUM=0
:ADDSPACEBACK
SET /A INCNUM+=1
SET TEXT=%TEXT% 
IF NOT %INCNUM% GEQ %HALFSPACE% GOTO :ADDSPACEBACK

SET /A NUMCHECK=(TTLSPACE-STRLEN)%%2
IF %NUMCHECK% GEQ 1 SET TEXT= %TEXT%

SET %VAR2SET%=%TEXT%
GOTO :EOF


:StrLength
::StrLength(retVal,string)
set #=%2%
set length=0
:stringLengthLoop
if defined # (set #=%#:~1%&set /A length += 1&goto stringLengthLoop)
set "%~1=%length%"
GOTO :EOF


:CALC_NAMES
CALL :StrLength C1 %CUSTOMPROCESS1%
CALL :StrLength C2 %CUSTOMPROCESS2%
CALL :StrLength C3 %CUSTOMPROCESS3%
CALL :StrLength C4 %CUSTOMPROCESS4%
SET LAST=%C1%
IF %C2% GEQ %LAST% SET LAST=%C2%
IF %C3% GEQ %LAST% SET LAST=%C3%
IF %C4% GEQ %LAST% SET LAST=%C4%
IF %LAST% GEQ %SPACES% SET /A SPACES=LAST+2
GOTO :EOF

:SHOW_CUSTOM_PROCESSES
SET SPACER=                                  !
SET SHOWCUSTOMPROCESS1=%CUSTOMPROCESS1%%SPACER%
SET SHOWCUSTOMPROCESS2=%CUSTOMPROCESS2%%SPACER%
SET SHOWCUSTOMPROCESS3=%CUSTOMPROCESS3%%SPACER%
SET SHOWCUSTOMPROCESS4=%CUSTOMPROCESS4%%SPACER%
SET SHOWEXIT=EXIT%SPACER%
ECHO Please enter a number or the name of a process to kill:
ECHO !SHOWCUSTOMPROCESS1:~0,%SPACES%![1]
ECHO !SHOWCUSTOMPROCESS2:~0,%SPACES%![2]
ECHO !SHOWCUSTOMPROCESS3:~0,%SPACES%![3]
ECHO !SHOWCUSTOMPROCESS4:~0,%SPACES%![4]
ECHO !SHOWEXIT:~0,%SPACES%![x]
GOTO :EOF